package com.wl.tp.protocol;

public class DataBuilder {

	public static void copyBytes(byte[] sourceByte, int targetLength,
			byte[] targetByte) {
		for (int i = 0; i < targetLength; i++) {
			targetByte[i] = sourceByte[i];
		}
	}

	public static byte[] contactBytes(byte[] b1, byte[] b2) {
		int b1_len = 0;
		int b2_len = 0;
		if (b1 != null) {
			b1_len = b1.length;
		}
		if (b2 != null) {
			b2_len = b2.length;
		}
		byte[] conectb = new byte[b1_len + b2_len];

		for (int i = 0; i < b1_len + b2_len; i++) {
			if (i < b1_len) {
				conectb[i] = b1[i];
			} else if (b2 != null) {
				conectb[i] = b2[i - b1_len];
			}
		}
		return conectb;
	}

	public static byte[] contactAllBytes(byte[][] allBytes) {
		byte[] result = contactBytes(allBytes[0], allBytes[1]);

		for (int i = 2; i < allBytes.length; i++) {
			result = contactBytes(result, allBytes[i]);
		}
		return result;
	}

	public static int getIntFromNetworkOrder(byte[] bytes, int index) {
		int intValue = 0;

		byte b;
		for (int i = 0; i < 4; ++i) {
			b = bytes[i + index];
			intValue += (b >= 0 ? b : b + 256) << 8 * (3 - i);
		}

		return intValue;
	}

	public static void setIntToNetworkOrder(byte[] bytes, int index,
			int intValue) {

		for (int i = 0; i < 4; ++i) {
			byte b = (byte) (intValue >>> ((3 - i) * 8));
			bytes[i] = b;
		}

	}

	public static float bytesToFloat(byte[] bytes, int index) {
		int intBits = 0;
		return Float.intBitsToFloat(intBits);
	}

	public static byte[] floatToBytes(float f) {
		byte[] bytes = new byte[4];
		int intBits = Float.floatToIntBits(f);

		DataBuilder.setIntToNetworkOrder(bytes, 0, intBits);

		return bytes;
	}

	public static long getLongFromNetworkOrder(byte[] bytes, int index) {

		long longValue = 0;

		int i1 = getIntFromNetworkOrder(bytes, index);

		int i2 = getIntFromNetworkOrder(bytes, index + 4);

		long l2 = i2 >= 0 ? i2 : 4294967296L + i2;

		long l1 = (long) i1 << 32;

		longValue = l1 + l2;

		return longValue;
	}

	public static void setLongToNetworkOrder(byte[] bytes, int index,
			long longValue) {
		for (int i = 0; i < 8; ++i) {
			byte b = (byte) (longValue >>> ((7 - i) * 8));
			bytes[i] = b; // >= 0 ? b : (byte)(b - 256 - 127);
		}
	}

	public static String getStringFromNetworkBytes(byte[] bytes, int index,
			int[] costBytes) {
		try {
			int length = DataBuilder.getUnShortFromNetworkOrder(bytes, index);
			index += 2;

			String result = "";
			if (length > 0) {
				result = new String(bytes, index, length, "gbk");
			}

			costBytes[0] = length + 2;

			return result;
		} catch (java.io.UnsupportedEncodingException nee) {
			throw new RuntimeException(nee.getMessage(), nee);
		}
	}

	public static byte[] getGBStringBytes(String value) {
		try {
			byte[] stringBytes = value.getBytes("gbk");
			int c = 2;
			byte[] lengthBytes = new byte[c];
			DataBuilder.setUnShortToNetworkOrder(lengthBytes, 0,
					stringBytes.length);
			byte[] result = new byte[stringBytes.length + c];
			for (int i = 0; i < c; ++i) {
				result[i] = lengthBytes[i];
			}
			for (int i = 0; i < stringBytes.length; ++i) {
				result[c + i] = stringBytes[i];
			}

			return result;
		} catch (java.io.UnsupportedEncodingException nee) {
			throw new RuntimeException(nee.getMessage(), nee);
		}
	}

	public static byte[] setCharToNetworkOrder(byte ch) {
		byte[] bch = new byte[1];
		bch[0] = (byte) (ch & 0xff);
		return bch;

	}

	public static int getUnShortFromNetworkOrder(byte[] bytes, int index) {
		int intValue = 0;
		// logger.info("bytes length" + bytes.length);
		// logger.info("index:" + index);
		byte b;
		for (int i = 0; i < 2; ++i) {
			b = bytes[i + index];
			intValue += (b >= 0 ? b : b + 256) << 8 * (1 - i);
		}

		return intValue;
	}

	public static void setUnShortToNetworkOrder(byte[] bytes, int index,
			int intValue) {
		byte[] newBytes = new byte[4];
		setIntToNetworkOrder(newBytes, 0, intValue);
		bytes[index] = newBytes[2];
		bytes[index + 1] = newBytes[3];
	}

	public static long getUnIntegerFromNetworkOrder(byte[] bytes, int index) {
		long longValue = 0;

		long b;
		for (int i = 0; i < 4; ++i) {
			b = bytes[i + index];
			longValue += (b >= 0 ? b : b + 256) << 8 * (3 - i);
		}

		return longValue;
	}

	public static void setUnIntegerToNetworkOrder(byte[] bytes, int index,
			long longValue) {
		byte[] newBytes = new byte[8];
		setLongToNetworkOrder(newBytes, 0, longValue);
		bytes[index] = newBytes[4];
		bytes[index + 1] = newBytes[5];
		bytes[index + 2] = newBytes[6];
		bytes[index + 3] = newBytes[7];
	}

	public static void main(String[] args) {
		byte[] testb = new byte[4];
		setIntToNetworkOrder(testb, 0, 127);
		for (int i = 0; i < testb.length; ++i) {
			System.out.println(testb[i]);
		}

	}

}
